<?php
class v_mysql extends DB{
	public function __construct(& $CONFIGS){
		foreach( $this->db_cfg as $k=>$v ){
			$this->db_cfg[$k] = $CONFIGS[$k];
		}
		$this->conn = $this->connect();
		$this->select_db();
		$this->query("SET NAMES UTF8");
	}

	private function connect(){
		$rs = @mysql_connect($this->db_cfg['dbHost'].':'.$this->db_cfg['dbPort'],$this->db_cfg['dbUser'],$this->db_cfg['dbPwd']);	
		if( $rs===false ){
			echo '<b>Error</b>: '.mysql_error(); 
			die();		
		}
		return $rs;
	}

	private function select_db(){
		$db = $this->db_cfg['dbName'];
		if( @mysql_select_db($db,$this->conn) ){
			return;
		}else{
			echo '<b>Error</b>: '.mysql_error(); 
			die();		
		}
	}

	/**
	 * @brief query 执行mysql查询
	 * @param string $sql sql语句
	 * @return resource OR bool
	 * @version v1.0
	 * @date 2012-03-19
	 */
	public function query( $sql="" ){
		if( !empty($sql) ){
			$this->sql = $sql;
		}else{
			$sql = $this->sql;
		}
		return mysql_query($sql,$this->conn);
	}

	/**
	 * @brief fetch 将结果集生成数组
	 * @param resource $result mysql查询结果集
	 * @param int $result_type
	 * @return array OR false
	 * @version v1.0
	 * @date 2012-03-19
	 */
	public function fetch( $result, $result_type=MYSQL_BOTH ){
		return mysql_fetch_array($result, $result_type);
	}

	/**
	 * @brief insert_id 返回插入的自增ID
	 * @return int or BIGINT
	 * @version v1.0
	 * @date 2012-03-19
	 */
	public function insert_id(){
		return mysql_insert_id($this->conn);
		//return $this->query('SELECT LAST_INSERT_ID();');
	}

	public function affected_rows(){
		return mysql_affected_rows($this->conn);
	}

	public function close(){
		return mysql_close($this->conn);
	}

	public function error(){
		return mysql_errno($this->conn).':'.mysql_error($this->conn);
	}
		
}
